package com.liuz.video.service.impl;

import blade.kit.http.HttpRequest;
import com.liuz.common.util.JsonUtil;
import com.liuz.common.util.TextUtil;
import com.liuz.video.model.VideoModel;
import com.liuz.video.service.VideoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**  
* @comment
* @author tanran 
* @date 2019年6月14日  
* @version 1.0  
*/
@Service
public class DouyinServiceImpl implements VideoService{
    private Logger logger = LoggerFactory.getLogger(DouyinServiceImpl.class);
	@Override
	public VideoModel parseUrl(String url) {
	    logger.info("Douyin_Start_ing");
        // TODO Auto-generated method stub
        VideoModel videoModel=new VideoModel();
        HttpRequest request;
        String res;
        String awemeId;
        if(url.indexOf("iesdouyin.com") == -1) {
            request= HttpRequest.get(url);
            res = request.body();
            awemeId=TextUtil.getSubString(res, "https://www.iesdouyin.com/share/video/", "/?");
        }else {
            awemeId=TextUtil.getSubString(url, "https://www.iesdouyin.com/share/video/", "/?");
        }

        url="https://www.iesdouyin.com/share/video/" + awemeId +"/?mid";
        request=HttpRequest.get(url).header("User-Agent","Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Mobile Safari/537.36");
        res = request.body();
//		System.out.println("1111:::"+res);
        String dytk_re=TextUtil.getSubString(res, "dytk: \"", "\" }");
//		System.out.println("2222:::"+dytk_re);
        url ="https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=" +
                awemeId + "&dytk=" + dytk_re;
        request=HttpRequest.get(url);
        res = request.body();
//		System.out.println(res);
        String temp_url = JsonUtil.getJsonValue(res, "item_list[0].video.play_addr.url_list[0]");
        request=HttpRequest.get(temp_url).header("User-Agent","Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Mobile Safari/537.36");
        String res_temp = request.body();
//		System.out.println(res_temp);
        String title = JsonUtil.getJsonValue(res, "item_list[0].desc");
        String playAddr = TextUtil.getSubString(res_temp, "<a href=\"", "\">Found</a>.");
        String cover = JsonUtil.getJsonValue(res, "item_list[0].video.origin_cover.url_list[0]");
        videoModel.setName(title);
        videoModel.setPlayAddr(playAddr);
        videoModel.setCover(cover);

//		url ="https://api-hl.amemv.com/aweme/v1/aweme/detail/?aid=1128&app_name=aweme&version_code=251&aweme_id="+awemeId;
//		request=HttpRequest.get(url).header("User-Agent","Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");
//		res = request.body();
//		System.out.println(res);
//	    String title = JsonUtil.getJsonValue(res, "aweme_detail.share_info.share_title");
//	    String playAddr = JsonUtil.getJsonValue(res, "aweme_detail.video.play_addr.url_list[0]");
//	    String cover = JsonUtil.getJsonValue(res, "aweme_detail.video.origin_cover.url_list[0]");
//	    videoModel.setName(title);
//	    videoModel.setPlayAddr(playAddr);
//	    videoModel.setCover(cover);
        return videoModel;
	}

	public static void main(String[] args) {
		System.out.println(new DouyinServiceImpl().parseUrl("http://v.douyin.com/f9qvFc/"));
		//http://v.douyin.com/UrVysU/
		//yes//http://v.douyin.com/6339jV/

	}
}
